package com.lry.basic.jvm.asm.transform_class.demo15;


import aj.org.objectweb.asm.ClassVisitor;
import aj.org.objectweb.asm.MethodVisitor;
import jdk.internal.org.objectweb.asm.util.Printer;

import static aj.org.objectweb.asm.Opcodes.ACC_ABSTRACT;
import static aj.org.objectweb.asm.Opcodes.ACC_NATIVE;

public class MethodFindInvokeVisitor extends ClassVisitor {
    private final String methodName;
    private final String methodDesc;

    public MethodFindInvokeVisitor(int api, ClassVisitor classVisitor,
                                   String methodName, String methodDesc) {
        super(api, classVisitor);
        this.methodName = methodName;
        this.methodDesc = methodDesc;
    }


    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String sig, String[] exceptions) {
        if(methodName.equals(name)&&methodDesc.equals(desc)){
            return new MethodFindInvokeAdapter(api, null);
        }
        return null;
    }

    private class MethodFindInvokeAdapter extends MethodVisitor{

        public MethodFindInvokeAdapter(int api, MethodVisitor methodVisitor) {
            super(api, methodVisitor);
        }

        @Override
        public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
            String info = String.format("%s %s.%s%s",
                    Printer.OPCODES[opcode], owner, name, descriptor);
            System.out.println(info);
            super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
        }
    }
}
